(function(){
		//拿到轮播图容器
		var api = 'http://h6.duchengjiu.top/shop/';
		var oBox = document.getElementById('lunbo');
		var oUl = oBox.getElementsByTagName('ul')[0];
		var oBanner = document.getElementById('banner');
		var oNext = document.getElementById('next');
		var oPre = document.getElementById('pre');
		var oNum = document.getElementById('number');
		//请求banner数据
		$.get(api + 'api_position.php', {}, function(result){
			//console.log(result);
			positionId = result.data[0].position_id;
			console.log(positionId);
			
			//在广告位编号里面再去请求具体广告位的数据 
			$.get(api + 'api_ad.php',{'position_id': positionId}, function(result){
				var data = result.data;
				console.log(data);
				for(var i = 0; i < data.length; i++){
					oBanner.innerHTML += '<li ><a href=""><img src="'+ data[i].url+'"/></a></li>'
					var span = document.createElement('span');
					oNum.appendChild(span);
				};
				var aNum = oNum.getElementsByTagName('span');
				aNum[0].className = 'num-active';
				var aLi = oBox.getElementsByTagName('li');
				//console.log(aLi);
				aLi[0].style.opacity='1';
				var liLength = aLi.length; 
				//console.log(liLength);
				var count = 0;
				var timer = setInterval(rightBtn, 2000)
					//鼠标移入
					oBox.onmouseover = function(){
						clearInterval(timer);
					};
					//鼠标移出
					oBox.onmouseout = function(){
						timer = setInterval(rightBtn, 2000)
					};
					function rightBtn(){
						//节流
						if(aLi[count].lock){return};
				
						//当前的li淡出
						animate(aLi[count], {'opacity': 0}, 1000);
				
						//下一个li淡入
						count++;//  
						if(count > liLength -1){
							count = 0;
						}
						animate(aLi[count], {'opacity': 1}, 1000);
						//调用数字
						num();
					};
					//右按钮点击
					oNext.onclick = rightBtn;
				
					//左按钮点击
					oPre.onclick = function(){
						//节流
						if(aLi[count].lock){return};
						//当前的li淡出
						animate(aLi[count], {'opacity': 0}, 500);
				
						//上一个li淡入
						count--;//  
						console.log(count); 
						if(count < 0){
							count = aLi.length-1;
						}
						animate(aLi[count], {'opacity': 1}, 500);
						//调用数字
						num();
					};
					//点击小方块  批量监听
					for(var i = 0; i < aNum.length; i++){
						aNum[i].index = i; //强制添加一个属性
						aNum[i].onclick = function(){
							//当前的li淡出
							animate(aLi[count], {'opacity': 0}, 500);
							
							//被点击的淡入
							count = this.index;
				
							animate(aLi[count], {'opacity': 1}, 500);
							//console.log(count, a)
							num();
						};
					}
					//数字跟随封装
					function num(){
						//数字跟随  排它法
						for(var i = 0; i < aNum.length; i++){
							//把所有全变为默认
							aNum[i].className = '';
						};
						
				
						//桥接
						var n = count;
						if(n == aLi.length){
							n = 0;
						};
						aNum[count].style.opacity = 0.3;
						aNum[count].className = 'num-active';
						animate(aNum[count], {'opacity': 1}, 500)
					};
			})
		});
		
		
		
		
	//请求分类
	(function(){
		//获取到要放入的容器
		var xiFen = document.getElementById('xifen');
		//console.log(classifyC);
		$.get(api + 'api_cat.php', {}, function(result){
			var data = result.data;
			//console.log(data);
			//把分类遍历出来
			var strA = '';
			for(var i = 0; i < data.length; i++){
				strA += '<li><a href="classify.html?cart_id='+ data[i].cat_id+'">' + data[i].cat_name+ '</a></li>';
			};
			//console.log(strA);
			xiFen.innerHTML += strA;
		});
			
	})();
	
	
	
		(function(){
		var hotContent = document.getElementById('goods-hot');
		
		var templateStr = document.getElementById('template').innerHTML;
		var pages=1;
		var pageCount=0;
		function getGoodsHot(pages){
			$.get(api + 'api_goods.php',{'page':pages,'pagesize':5},function(data){
				pageCount=data.page.page_count;
				var obj = data.data;
				var str='';
				var compile = _.template(templateStr);
				//console.log(obj);
				for(var i = 0; i < obj.length; i++){
					str += compile(obj[i]);
				}
				hotContent.innerHTML += str;
				var aLi = hotContent.getElementsByTagName('li');
				var aDiv = hotContent.getElementsByTagName('div');
				for(var j = 0;j<aDiv.length;j++){
					if((j+1)%5==1 ){
						aLi[j].className='sty_img';
						aLi[j].style.marginLeft='0';
						aDiv[j].className='sty_b';
					}else{
						//aLi[j].className='sty_l';
						aDiv[j].className='sty_l';
					}
				};
					
				$('[lazyloadsrc]').YdxLazyLoad();
			});
		};
		
		getGoodsHot(pages);
		var oMore =document.getElementById('more');
		oMore.onclick = function(){
			pages++;
			//console.log(pages);
			if(pages >pageCount){
				oMore.inner='没有更多了';
				return
			};
			getGoodsHot(pages);
		};
	})();
	
	})();